// const BASE_URL_TEST = 'http://goapi.sanjitian.cn/api'
const BASE_URL_TEST = import.meta.env.VITE_BASE_PATH


export const getRolesPage = async () => {
    try {
        const res = await fetch(BASE_URL_TEST + '/roles/page?pageNum=1&pageSize=10')
        if (res.ok) {
            const allJson = await res.json()
            return allJson
        }
    } catch (error) {
        console.error('请求出错', error);

    }
}

export const postRolesPage = async () => {
    const myHeaders = new Headers();
    // 设置请求携带的数据格式是 JSON格式
    myHeaders.append("Content-Type", "application/json");
    // 添加的 自定义的 header 信息（通常用户使用的秘钥会放到这里，后端接受之后用于判断用户是否登录）
    myHeaders.append(
        'Authorization',
        'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJSW5mbyI6eyJ1c2VySWQiOjIsInVzZXJuYW1lIjoiYWRtaW4iLCJuaWNrbmFtZSI6Iuezu-e7n-euoeeQhuWRmGFkbWluIiwiZGVwdElkIjoxLCJyb2xlcyI6IkFETUlOIn0sImlzcyI6ImR5czYyMzAiLCJleHAiOjE3Mjg5MDk5MTgsIm5iZiI6MTcyODg3MzkyOCwiaWF0IjoxNzI4ODczOTE4fQ.9xhdL57Qs38Mlm0K-TCwgrQjET1ZS2jUZhIOob0-pGk'
    )

    const myHeadersPost = JSON.stringify({
        id: 670,
        name: "你好啊",
        code: "670",
        sort: 670,
        status: 0,
        dataScope: 72
    })
    // fetch 配置项
    const requertpost = {
        // 请求方式 GET POST DELETE  PUT
        method: 'POST',
        // 设置 请求的 header 头信息
        headers: myHeaders,
        // 请求携带的数据 
        body: myHeadersPost
    }

    const res = await fetch("http://goapi.sanjitian.cn/api/roles", requertpost)

    if (res.ok) {
        const allJson = await res.json()
        return allJson
    }
}

/**
 * 通过角色的id 修改角色的数据
 */
export const PutRolesPage = async () => {
    const myHeaders = new Headers();
    myHeaders.append("Content-Type", "application/json");

    const myHeadersPut = JSON.stringify({
        name: "郭浩发呆中",
        code: "109",
        sort: 46,
        status: 41,
        dataScope: 44
    });

    const requertput = {
        method: 'PUT',
        headers: myHeaders,
        body: myHeadersPut
    }

    const res = await fetch("http://goapi.sanjitian.cn/api/roles/109", requertput)

    if (res.ok) {
        const allJson = await res.json()
        return allJson
    }
}
// 删除角色
export const DeleteRolesPage = async () => {
    const myHeaders = new Headers();
    myHeaders.append("Content-Type", "application/json");
    myHeaders.append("User-Agent", "Apifox/1.0.0 (https://apifox.com)");
    myHeaders.append("Authorization", "");

    const requertput = {
        method: 'DELETE',
        headers: myHeaders,
        redirrct: 'follow'
    }

    const res = await fetch("http://goapi.sanjitian.cn/api/roles/108", requertput)

    if (res.ok) {
        const allJson = await res.json()
        return allJson
    }
}

export const SaveRolesPage = async () => {
    const myHeaders = new Headers();
    myHeaders.append("Content-Type", "application/json");
    myHeaders.append("User-Agent", "Apifox/1.0.0 (https://apifox.com)");
    myHeaders.append("Authorization", "");

    const raw = JSON.stringify({
        id: 289,
        username: "666",
        nickname: "喵新人",
        email: "88889999",
        gender: 1,
        Password: "123456"
    });

    const requertput = {
        method: 'PATCH',
        headers: myHeaders,
        body: raw,
    };

    const res = await fetch("http://goapi.sanjitian.cn/api/roles/save", requertput)

    if (res.ok) {
        const allJson = await res.json()
        return allJson
    }
}
